package org.zjx.route.model;

import com.alibaba.fastjson2.annotation.JSONType;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.zjx.route.entity.Location;
import org.zjx.route.entity.RouteType;

/**
 * 跑路路由类
 * 表示同场景内移动的路径
 */

@JSONType(typeName = "RunningRoute")
@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class RunningRoute extends AbstractRoute {
    private static final double OUTDOOR_SPEED = 2; // 室外速度(单位距离/秒)
    private static final double INDOOR_SPEED = 1.5; // 室内速度

    private boolean indoor;  // 是否室内

    /**
     * 构造函数
     *
     * @param start  起点
     * @param end    终点
     * @param indoor 是否室内
     * @throws IllegalArgumentException 如果不在同一场景
     */
    public RunningRoute(Location start, Location end, boolean indoor) {
        super(start, end, calculateTime(start, end, indoor), RouteType.RUNNING, false);
        if (!start.sameScene(end)) {
            throw new IllegalArgumentException("跑路只能在同场景内进行");
        }
        this.indoor = indoor;
    }

    /**
     * 计算跑路耗时
     *
     * @param start  起点
     * @param end    终点
     * @param indoor 是否室内
     * @return 耗时(秒)
     */
    private static int calculateTime(Location start, Location end, boolean indoor) {
        double distance = start.distanceTo(end); // 转换为公里
        double speed = indoor ? INDOOR_SPEED : OUTDOOR_SPEED;
        return Math.max((int) Math.round(distance / speed), 1);
    }

    /**
     * 创建反向路由
     *
     * @return 反向路由
     */
    @Override
    protected Route createReverseRoute() {
        return new RunningRoute(getEnd(), getStart(), indoor);
    }

    /**
     * 获取路由描述
     *
     * @return 描述字符串
     */
    @Override
    public String getDescription() {
        return String.format("从%s跑路到%s(%s)，耗时%d秒",
                getStart(), getEnd(), indoor ? "室内" : "室外", getTime());
    }
}